package com.wlcorp.forcazamis.web.action.system;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.wlcorp.forcazamis.business.system.IDepartmentService;
import com.wlcorp.forcazamis.domain.system.Department;
import com.wlcorp.forcazamis.domain.system.Staff;
import com.wlcorp.forcazamis.sys.util.AppConstant;
import com.wlcorp.forcazamis.sys.util.ContextUtil;
import com.wlcorp.forcazamis.sys.util.WebUtil;

public class DepartmentAction extends DispatchAction {
	public ActionForward getDepartmentList(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		List<Department> departmentList = ContextUtil.getInstance().getDepartmentService().getDepartmentList(null);
		request.setAttribute("departmentList",departmentList);
		return mapping.findForward("list");
	}
	
	public ActionForward getDepartment(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Department department = new Department();
		WebUtil.setRequestToObject(request,department);
		department = ContextUtil.getInstance().getDepartmentService().getDepartmentById(department.getDepartmentId());
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(new Staff());
		if(null == department){
			department = new Department();
		}
		request.setAttribute("staffList",staffList);
		request.setAttribute("department",department);
		return mapping.findForward("modify");
	}
	
	public ActionForward modifyDepartment(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Department department = new Department();
		WebUtil.setRequestToObject(request,department);
		ContextUtil.getInstance().getDepartmentService().saveDepartment(department);
		List<Staff> staffList = ContextUtil.getInstance().getStaffService().getStaffList(new Staff());
		WebUtil.setAlertMessage(request,"操作成功！");
		request.setAttribute("staffList",staffList);
		request.setAttribute("department",department);
		return mapping.findForward("modify");
	}
	
	public ActionForward deleteDepartment(ActionMapping mapping,ActionForm form,
			HttpServletRequest request,HttpServletResponse response){
		Department department = new Department();
		WebUtil.setRequestToObject(request,department);
		IDepartmentService service = ContextUtil.getInstance().getDepartmentService();
		department = service.getDepartmentById(department.getDepartmentId());
		if(null != department){
			service.deleteDepartment(department);
			if(AppConstant.BASE_BEAN_STATUS_ERROR == department.getStatus()){
				WebUtil.setAlertMessage(request,department.getMsg());
			}
		}
		return getDepartmentList(mapping,form,request,response);
	}
}
